#include "bullet.h"
#include "constant.h"
#include "globals.h"

Bullet::Bullet()
{
    _box.x = 0;
    _box.y = 0;

    _box.w = BULLET_WIDTH;
    _box.h = BULLET_HEIGHT;

    _xVel = 0;
    _yVel = 0;
}

void Bullet::set(int x, int y, int xVel, int yVel)
{
    _box.x = x;
    _box.y = y;

    _box.w = BULLET_WIDTH;
    _box.h = BULLET_HEIGHT;

    _xVel = xVel;
    _yVel = yVel;
}

void Bullet::move()
{
    _box.x += _xVel;
    _box.y += _yVel;

    if ( ( _box.x < 0 ) || ( _box.x > SCREEN_WIDTH ) ||
         ( _box.y < 0 ) || ( _box.y > SCREEN_HEIGHT ) )
    {
        int x, y;
        int xVel = 0, yVel = 0;

        if (rand() % 4 == 0)
        {
            x = 0;
            y = rand() % (SCREEN_HEIGHT + 1);
            xVel = rand() % 2 + 1;
            yVel = rand() % 5 - 2;
        }
        else if (rand() % 4 == 1)
        {
            x = SCREEN_WIDTH;
            y = rand() % (SCREEN_HEIGHT + 1);
            xVel = rand() % 2 - 2;
            yVel = rand() % 5 - 2;
        }
        else if (rand() % 4 == 2)
        {
            x = rand() % (SCREEN_WIDTH + 1);
            y = 0;
            xVel = rand() % 5 - 2;
            yVel = rand() % 2 + 1;
        }
        else
        {
            x = rand() % (SCREEN_WIDTH + 1);
            y = SCREEN_HEIGHT;
            xVel = rand() % 5 - 2;
            yVel = rand() % 2 - 2;
        }

        set(x, y, xVel, yVel);
    }
}

void Bullet::show()
{
    //Show the dot
    apply_surface( _box.x, _box.y, bullet, screen );
}

SDL_Rect Bullet::getBox()
{
    return _box;
}
